<template>
  <el-dialog
    title="JSA编号选择"
    append-to-body
    :visible.sync="dhjobJsaBox"
    width="60%"
    :before-close="cancelChooseControlJsa"
  >
    <avue-crud
      :option="jsaoption"
      :table-loading="jsaloading"
      :data="jsadata"
      :page.sync="jsapage"
      :before-open="jsaeforeOpen"
      ref="jsacrud"
      @search-change="jsasearchChange"
      @search-reset="jsasearchReset"
      @selection-change="jsaselectionChange"
      @current-change="jsacurrentChange"
      @size-change="jsasizeChange"
      @refresh-change="jsarefreshChange"
      @on-load="jsaonLoad"
    >
    </avue-crud>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="cancelChooseControlJsa()">取 消</el-button>
      <el-button type="primary" @click="chooseControlJsa()">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getList,getDetail} from "@/api/xcgl/jsaanalysis";
    import {jsaColumn} from "../constant/jsa";

    export default {
      name: "dhjobJsa",
      props: {
        dhjobJsaBox: {default: false},
      },
      data(){
        return{
          jsaform:{},
          jsaquery: {},
          jsaloading: true,
          jsapage: {
            pageSize: 10,
            currentPage: 1,
            total: 0
          },
          jsaselectionList: [],
          jsaoption: {
            menu:false,
            addBtn:false,
            calcHeight: 30,
            tip: false,
            searchShow: true,
            searchMenuSpan: 6,
            border: true,
            index: true,
            viewBtn: true,
            selection: true,
            dialogClickModal: false,
            column: jsaColumn
          },
          jsadata: []
        }
      },
      // watch: {
      //   // JSA编号选择显隐
      //   dhjobJsaBox() {
      //     if(this.dhjobJsaBox === true) {
      //       this.initData();
      //     }
      //   }
      // },
      computed: {
        ids() {
          let ids = [];
          this.jsaselectionList.forEach(ele => {
            ids.push(ele.id);
          });
          return ids.join(",");
        }
      },
      methods:{
        //选择JSA
        chooseControlJsa(){
          if(this.jsaselectionList.length === 0) {
            this.$message.warning("请选择JSA!");
            return;
          }
          this.$emit('chooseControlJsa', this.jsaselectionList);
          this.jsaselectionClear()
        },
        cancelChooseControlJsa() {
          this.jsaselectionClear()
          this.$emit("cancelChooseControlJsa")
        },
        // 关闭选择管控措施弹窗
        handleCloseChooseJsa(done) {
          this.jsaselectionClear()
          done();
          this.$emit('handleCloseChooseJsa');

        },

        jsaeforeOpen(done, type){
          if (["edit", "view"].includes(type)) {
            getDetail(this.jsaform.id).then(res => {
              this.jsaform = res.data.data;
            });
          }
          done();
        },
        jsasearchReset() {
          this.jsaquery = {};
          this.jsaonLoad(this.jsapage);
        },
        jsasearchChange(params, done) {
          this.jsaquery = params;
          this.jsapage.currentPage = 1;
          this.jsaonLoad(this.jsapage, params);
          done();
        },
        jsaselectionChange(list) {
          this.jsaselectionList = list;
        },
        jsaselectionClear() {
          this.jsaselectionList = [];
          this.$refs.jsacrud.toggleSelection();
        },
        jsacurrentChange(currentPage){
          this.jsapage.currentPage = currentPage;
        },
        jsasizeChange(pageSize){
          this.jsapage.pageSize = pageSize;
        },
        jsarefreshChange() {
          this.jsaonLoad(this.jsapage, this.jsaquery);
        },
        jsaonLoad(page, params = {}) {
          this.jsaloading = true;
          getList(page.currentPage, page.pageSize, Object.assign(params, this.jsaquery)).then(res => {
            const data = res.data.data;
            this.jsapage.total = data.total;
            this.jsadata = data.records;
            this.jsaloading = false;
            this.jsaselectionClear();
          });
        }
      }
    }
</script>

<style scoped>

</style>
